package com.example.mapreducr.writable;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * ClassName: WordCountReduce
 * Package: com.example.mapreducr.wordcount
 * Description:
 * User: fzykd
 *
 * @Author: LQH
 * Date: 2023-07-15
 * Time: 20:43
 */

/**
 * 1.自定义Reducer要继承自己的父类
 * 2.Reducer的输入类型是Mapper的输出类型
 * 3.业务逻辑在reduce方法值写 相同的key执行一次reduce方法
 */
public class WordCountReduce extends Reducer<Text, FlowBean,Text,FlowBean> {

    private FlowBean outV = new FlowBean();

    @Override
    protected void reduce(Text key, Iterable<FlowBean> values, Context context) throws IOException, InterruptedException {

        //相同的key执行一次 reduce
        //文本中可能存在相同的电话号码作为key
        //相加他们的 up down

        long totalUp = 0;
        long totalDown = 0;
        //1.遍历values 将up 和 down 分别累加
        for (FlowBean flowBean : values){
            totalUp += flowBean.getUp();
            totalDown += flowBean.getDown();
        }
        //封装outKV
        outV.setUp(totalUp);
        outV.setDown(totalDown);
        outV.setSum();
        //写出 key就是唯一的电话号码
        context.write(key,outV);



    }
}
